<template>
	<section>
		<div class="address-contain">	
			<dl class="border-b20 pt30rem pb30rem bg-white" v-for="(item, index) in other_list" v-if="flag" >
				<div @click="sectionAddress(item)">
					<dt class="bold fs28rem c333 pb25rem pl30rem pr30rem">
						{{item.username}}
						&emsp;
						{{item.moblie}}
					</dt>
					<dd class="fs26rem c666 pb25rem border-b">	
						<div class="pl30rem pr30rem ">
							{{item.pcd_address}}&nbsp;{{item.address}}
						</div>					
					</dd>
				</div>
				<dd class="pt30rem pl30rem pr30rem layout">	
					<div class="pretty p-icon p-round p-jelly">
				        <input type="checkbox" name="type" v-model="item.is_default" @click="check(!item.is_default, item.id)"/>
				        <div class="state p-primary">
				            <i class="iconfont icon-gouxuan1 icon mdi mdi-check cxo-red"></i>
				            <label>
				            	<span class="fs28rem">
						            设为默认地址
						        </span>
						    </label>
				        </div>
				    </div>							
					<div class="fs25rem c666 bold layout">	
						<button @click="edit(item)">
							编辑
						</button>						
						&emsp;
						<span @click="deletes(item.id, index)">
						删除
						</span>					
					</div>						
				</dd>
			</dl>
			<template v-else>
				<default :imgUrl="img" explain='暂无添加地址'></default>
			</template>
			<div style="height:3rem"></div>
			<div class="fixed flex f5f5f5" :class="{bottom5 : showApp }" @click="$router.replace('/member/address/AddAdress?goodsId=' + goodsId)">
				<button class="mt20rem mr30rem ml30rem mb20rem btn cxo-red align-center fs30rem white flex_1 radius">	
					新增收货地址
				</button>	
			</div>
		</div>
	</section>
</template>

<script>

	import { getAddress, deleteAddress, defaultAddress } from "@/api/address"; 
	import { getFreeLottery } from "@/api/goods-detail";
	import { bandingMobile } from "@/api/login";
	import Default from "@/components/Default";
	import $toast   from '@/utils/toast';
	import $Modal	from '@/utils/modal';

	export default
	{
		components: 
		{
			Default
		},
	   	data()
		{
		    return{
				uid        : localStorage.getItem('uid'),
				mobile     : localStorage.getItem('mobile'),
				img        : require('../../../assets/icon-address.png'),
				flag       : true,
				other_list : [],
				goodsId    : this.$route.query.goods_id,
				type       : this.$route.query.type,
				showApp    : window.sessionStorage.getItem('isapp'),
            }
		},
		methods:
		{
			loadData()
			{
				getAddress(this.uid).then( res => {
					if(res.data.length > 0)
						this.other_list = res.data; 
					else
					 	this.flag = false;
					 
					if(!this.mobile)
						this.$router.replace('/login/banding?redirect=' + this.$router.currentRoute.fullPath)
				});
			},
			deletes(id, i)
			{
				$Modal.confirm("确定删除该地址吗？").then(res => { 
					deleteAddress( this.uid, id).then( res => { 
						this.other_list.splice(i, 1);
					});
				});					
			},
			edit(index,id)
			{
				this.$router.push({ path: '/member/address/AddAdress', query: { key: index + 1,id: id } });
			},
			check(check_val,id)
			{
				this.other_list.forEach( (item, key) =>{					
					if( id != item.id)
					item.is_default = 0;
				});
				let isDefaultAddress = check_val ? 1 : 0;
				defaultAddress( this.uid, id, isDefaultAddress).then( res => {
					$toast("设置成功")
				});
			},
			sectionAddress(item)
			{
				this.goodsId = this.$route.query.goodsId ? this.$route.query.goodsId : this.$store.state.goods_id;

				if(this.goodsId)
				{
					this.$store.dispatch("addressId", item.id);
					switch(this.type)
					{
						case '-2':
							this.$router.replace({ path:"../../../detail/lottery-detail?" + 'goodsId=' + this.goodsId + '&addressId=' + item.id});
						return;
						case '-3':
							getFreeLottery({uid:this.uid,goods_id:this.goodsId,address_id:item.id}).then( res => {
								this.$router.replace({ path:"../../../detail/free-detail?" + 'goodsId=' + this.goodsId})
							})
						return;
						case '-4':
							this.$router.replace({ path:"../../../detail/share-detail?" + 'goodsId=' + this.goodsId + '&addressId=' + item.id});
						return;
					}
				}else{
					//预约活动跳转
					switch(this.$route.query.type)
					{
						case "experience":
							this.$store.dispatch('selectAddress',item);
							this.$router.replace('/detail/experience-detail/'+ this.$route.query.id + '?status=2')
							return new Promise(() => {});
						case "presell":
							this.$store.dispatch('paySelectAddress',item);
							this.$router.replace('/shopping/pay?type=2');
							return new Promise(() => {});
						case "pay":
							this.$store.dispatch('paySelectAddress',item);
							this.$router.replace('/shopping/pay');
							return new Promise(() => {});
						case "groupPay":
							this.$store.dispatch('paySelectAddress',item);
							this.$router.replace('/shopping/group-pay');
							return new Promise(() => {});
					}
					
					// if( this.$route.query.type == "experience")
					// {	
					// 	this.$store.dispatch('selectAddress',item);
					// 	this.$router.replace('/detail/experience-detail/'+ this.$route.query.goods_id + '?status=2')
					// }
					// else if(this.$route.query.type == "pay")
					// {
					// 	this.$store.dispatch('paySelectAddress',item);
					// 	this.$router.replace('/shopping/pay');
					// }
					// else if(this.$route.query.type == "groupPay")
					// 	this.$store.dispatch('paySelectAddress',item);
					// 	this.$router.replace('/shopping/group-pay');
				}
			},
			edit(item)
			{
				this.$store.dispatch('addressList',item);
				this.$router.replace("/member/address/AddAdress?key=1");
			}
		},
		created()
		{
			this.loadData();
		},
		watch: {
			'$route'()
			{
				this.loadData();
			}
		}
	}
</script>

<style lang="less">
	.address-contain{
		background-color: #f5f5f5;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		.add-new-address-btn{
			position: fixed;
			bottom: 5px;
			width: 92%;
		}
		.f5f5f5{
			background-color: #f5f5f5;
		}
		.bottom5{
			bottom:5rem;
		}
	}
</style>

